/**
 * 聊呗登录按钮控件
 */
var LiaoBeiLoginUnit = LoginBtnUnit.extend({
    _reciveCodeKey: EventKey.ReceiveLiaobeCode,
    _authReq: 'LiaobeiCode',
    _callback:null,

    redirectLogin: function (callBack) {
        LoginBtnUnit.prototype.redirectLogin.call(this);
        if (!this._isInstalled()) {
            this.showNoInstalledAlert();
            return;
        }
        this._callback = callBack;
        showLoading('拉取聊呗授权中..');
        LiaoBeUtil.redirectLogin();
    },
    _receiveCode: function (code) {
        LoginBtnUnit.prototype._receiveCode.call(this);
        if (isNullString(code)) {
            return;
        }
        this._requestAuth(code);
    },
    _requestAuthSuccess: function (response) {
        var userInfo = JSON.parse(response);
        switch (userInfo.result) {
            case 0:
                gameData.lbToken = userInfo.liaobei.token;
                gameData.lbOpenid = userInfo.liaobei.openId;
                if(this._callback){
                    this._callback();
                }
                break;
            case 2:
                gameData.lbToken = userInfo.token;
                gameData.lbOpenid = userInfo.openid;
                if(this._callback){
                    this._callback();
                }
                // WeChatUtil.redirectLogin("LiaobeiData", userInfo.data);
                break;
            default:
                hideLoading();
                cc.sys.localStorage.removeItem('lb_openid');
                showAlert('聊呗授权验证失败!');
                break;
        }
    },
    _requestAuthFailure: function () {
        LoginBtnUnit.prototype._requestAuthFailure.call(this);
        showAlert('拉取聊呗授权失败,请重新登录!');
    },

    /**
     * 是否安装
     * @returns {boolean}
     * @protected
     */
    _isInstalled: function () {
        return LiaoBeUtil.isInstalled();
    },
    /**
     * 未安装提示
     */
    showNoInstalledAlert: function () {
        showAlert('您没安装聊呗!,点击确定去下载聊呗。', function () {
            cc.sys.openURL("https://www.liaobe.cn/");
        });
    }
});